﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using DataAccessLayer;

namespace Stationery_Store_Inventory_Management
{
    public partial class DisbursementList : System.Web.UI.Page
    {
        DisbursementListController dCont = new DisbursementListController();
        int colPointId;
        String userName;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                #region Check if user exists and has permission to view this page
                if (Session["UserLoggedIn"] != null)
                {
                    List<string> list = Session["UserLoggedIn"] as List<string>;
                    if (list[0].Equals("validuser"))
                    {
                        int roleId = Convert.ToInt32(list[6]);
                        if (roleId != 5)
                        {
                            Response.Redirect("/AccessDenied.aspx");
                        }
                    }
                    else
                    {
                        Response.Redirect("/Login.aspx");
                    }
                }
                else
                {
                    Response.Redirect("/Login.aspx");
                }
                #endregion

                if (!IsPostBack)
                {
                    List<CollectionPoint> collectionPoints = dCont.getCollectionPointNamesCont();
                    DropDownList1.DataValueField = "CollectionPointID";
                    DropDownList1.DataTextField = "CollectionPointName";
                    DropDownList1.DataSource = collectionPoints;
                    DropDownList1.DataBind();

                    colPointId = Convert.ToInt32(DropDownList1.SelectedValue);
                    var result = dCont.getDeptDetailsCont(colPointId);
                    DeptByCollectionPointGrid.DataSource = result;
                    DeptByCollectionPointGrid.DataBind();
                }
            }
            catch (Exception ex)
            {
            }
        }

        protected void DeptCoPointDetails_Click(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "DeptCoPointDetails")
                {
                    int index = Convert.ToInt32(e.CommandArgument);
                    string DepId = DeptByCollectionPointGrid.Rows[index].Cells[2].Text;
                    string Name = DeptByCollectionPointGrid.Rows[index].Cells[1].Text;
                    Response.Redirect("DisbursementDetailsList.aspx?ic=" + DepId + "&nm=" + Name +
                        "&ColPointName=" + DropDownList1.SelectedItem); 
                }
            }
            catch (Exception ex)
            {
            }
        }

        public void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                colPointId = Convert.ToInt32(DropDownList1.SelectedValue);
                var result = dCont.getDeptDetailsCont(colPointId);
                DeptByCollectionPointGrid.DataSource = result;
                DeptByCollectionPointGrid.DataBind();
            }
            catch (Exception ex)
            {
            }
        }      

        protected void btnraiseAdjustment_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Redirect("/UpdateDisbursementList.aspx");
            }
            catch (Exception ex)
            {
            }
        }

        protected void btnChangeCollectionPointDetails_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Redirect("/ChangeCollectionPointDetails.aspx");
            }
            catch (Exception ex)
            {
            }
        }

    }
}
